package 算法.练习1;

/**
 * @author:谢君臣
 * @Date:2021/8/2-14:28
 * @version:1.0
 * @function:
 */
public class NC88 {
    public static int res = 0;
    public static void main(String[] args) {
        int[] a = {14, 7, 16, 5, 3, 22, 8, 10};
        System.out.println(findKth(a, a.length, 3));
    }

    public static int findKth(int[] a, int n, int K) {
        // write code here
        QuickSort(a, 0, n - 1, K);
        return res;
    }

    private static void QuickSort(int[] a, int l, int r, int k) {
        if (l < r) {
            int i = l, j = r;
            while (i < j) {
                int temp = a[l];
                while (i < j && a[i] < temp) i++;
                while (i < j && a[j] >= temp) j--;
                swap(a, i, j);
            }
            swap(a, l, i);
            if (r-i+1>k){
                QuickSort(a, i+1 , r, k);
            }else if (r-i+1==k){
                res = a[i];
            }
            else {
                QuickSort(a, l, i-1, k-(r-i+1));
            }
        }
    }

    private static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}
